# -*- coding = utf-8 -*-
# @Time : 2022/1/17 19:02
# @Author : GHHHHHHHHH
# @File : floorFillColor.py
# @Software : PyCharm

import numpy as np
import cv2 as cv

from json_open_close import open_json


class FloodFillImg(object):
    def __init__(self, _map: np.asarray):
        self.map = _map
        self.xLength = self.map.shape[0]
        self.yLength = self.map.shape[1]
        self.colors = open_json("colors.json")
        self.BLACK = (0, 0, 0)
        self.WHITE = (255, 255, 255)
        self.checkMap()
        self.getUncoloredPoints()

    def checkMap(self):
        for i in range(self.xLength):
            for j in range(self.yLength):
                if tuple(self.map[i][j])[0] > 200 and tuple(self.map[i][j])[1] > 200 \
                        and tuple(self.map[i][j])[2] > 200:
                    self.map[i][j] = (255, 255, 255)
                elif tuple(self.map[i][j])[0] < 50 and tuple(self.map[i][j])[1] < 50 \
                        and tuple(self.map[i][j])[2] < 50:
                    self.map[i][j] = (0, 0, 0)

    def getUncoloredPoints(self):
        colorID = 0
        for x in range(self.xLength):
            for y in range(self.yLength):
                if tuple(self.map[x][y]) == self.BLACK:
                    self.map = self.floor_begin(y, x, self.colors[colorID])
                    colorID += 1

    def floor_begin(self, x, y, color):
        copyImage = self.map.copy()
        h, w = copyImage.shape[:2]
        mask = np.zeros([h + 2, w + 2], np.uint8)
        cv.floodFill(copyImage, mask, (x, y), color, (100, 100, 100), (50, 50, 50),
                     cv.FLOODFILL_FIXED_RANGE)
        return copyImage
